﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration


Partial Class userControls_eventCalendar
    Inherits System.Web.UI.UserControl


    Protected Sub calEvent_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calEvent.SelectionChanged
        ' Define data objects

        Dim conn As SqlConnection
        Dim comm As SqlCommand
        Dim reader As SqlDataReader
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("MMile").ConnectionString
        Dim eventDate As Date
        eventDate = calEvent.SelectedDate
        conn = New SqlConnection(connectionString)
        conn.Open()

        comm = New SqlCommand("SELECT * FROM Events WHERE'" & eventDate & "'  = EventDate", conn)
        reader = comm.ExecuteReader()

        eventDisplay.DataSource = reader
        eventDisplay.DataBind()

        reader.Close()
        conn.Close()

        If eventDisplay.Items.Count = 0 Then
            lblEvent.Text = "Sorry there are no events on this date"
        Else
            lblEvent.Text = ""
        End If

        FillEventDataset()


    End Sub

    Protected dsEvents As DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'FillEventNav()
        Dim EventID As String
        EventID = Request.QueryString("EventID")

        If EventID <> "" Then
            ' Define data objects
            Dim conn As SqlConnection
            Dim comm As SqlCommand
            Dim reader As SqlDataReader
            Dim connectionString As String = ConfigurationManager.ConnectionStrings("MMile").ConnectionString
            conn = New SqlConnection(connectionString)
            conn.Open()

            comm = New SqlCommand("SELECT * FROM Events WHERE EventID=@EventID", conn)
            comm.Parameters.Add("@EventID", System.Data.SqlDbType.NVarChar)
            comm.Parameters("@EventID").Value = EventID
            reader = comm.ExecuteReader()

            'eventDisplay.DataSource = reader
            'eventDisplay.DataBind()

            reader.Close()
            conn.Close()

        End If
        FillEventDataset()

        If Not IsPostBack Then
            calEvent.VisibleDate = DateTime.Today
            FillEventDataset()
        End If
    End Sub

    Protected Sub FillEventDataset()
        Dim firstDate As New DateTime(calEvent.VisibleDate.Year, calEvent.VisibleDate.Month, 1)
        Dim LastDate As DateTime = GetFirstDayOfNextMonth()
        dsEvents = GetCurrentMonthData(firstDate, LastDate)
    End Sub

    Protected Function GetFirstDayOfNextMonth() As DateTime
        Dim monthNumber, yearNumber As Integer
        If calEvent.VisibleDate.Month = 12 Then
            monthNumber = 1
            yearNumber = calEvent.VisibleDate.Year + 1
        Else
            monthNumber = calEvent.VisibleDate.Month + 1
            yearNumber = calEvent.VisibleDate.Year
        End If
        Dim lastDate As New DateTime(yearNumber, monthNumber, 1)
        Return lastDate
    End Function

    Protected Sub calEvent_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles calEvent.VisibleMonthChanged
        FillEventDataset()
    End Sub

    Function GetCurrentMonthData(ByVal firstDate As DateTime, ByVal lastDate As DateTime) As DataSet
        Dim dsMonth As New DataSet
        Dim connString As String = ConfigurationManager.ConnectionStrings("MMile").ConnectionString
        Dim dbConnection As New SqlConnection(connString)
        Dim query As String
        query = "SELECT EventDate FROM Events WHERE EventDate >= @firstdate AND EventDate < @lastDate"
        Dim dbCommand As New SqlCommand(query, dbConnection)
        dbCommand.Parameters.Add(New SqlParameter("@firstDate", firstDate))
        dbCommand.Parameters.Add(New SqlParameter("@lastDate", lastDate))

        Dim SqlDataAdapter As New SqlDataAdapter(dbCommand)
        Try
            SqlDataAdapter.Fill(dsMonth)
        Catch ex As Exception
        End Try
        Return dsMonth
    End Function



    Protected Sub calEvent_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles calEvent.DayRender
        Dim nextDate As DateTime
        If Not dsEvents Is Nothing Then
            For Each dr As DataRow In dsEvents.Tables(0).Rows
                nextDate = CType(dr("EventDate"), DateTime)
                If nextDate = e.Day.Date Then
                    If Date.Now > e.Day.Date Then


                        e.Cell.BackColor = System.Drawing.Color.LightSlateGray

                    Else
                        e.Cell.BackColor = System.Drawing.Color.DarkOrange
                        e.Cell.ForeColor = System.Drawing.Color.White
                    End If
                End If
            Next
        End If
    End Sub

End Class
